﻿using Common;
/*----------------------------------------------------------------
// Copyright (C) 2012 Q.FunnyFace
// 版权所有。 
//
// 文件名：ManagerUser.cs
// 文件功能描述：用户信息业务逻辑
//
// 
// 创建标识：2012-10-26 zhangqiang
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using DomainModel;
using IDataAccess;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Common.Extensions.OrderBy;

namespace Manager
{

    /// <summary>
    /// 用户信息业务逻辑
    /// </summary>
    public class ManagerUser : IManagerUser
    {

        /// <summary>
        /// 用户信息数据访问接口
        /// </summary>
        public IDataAccessUser IDAUser { get; set; }


        public IDataAccessCompany IDACompany { get; set; }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>用户信息</returns>
        public UserInfo GetUser(int userID)
        {
            return IDAUser.Find(userID);
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="loginName">用户登录名</param>
        /// <returns>用户信息</returns>
        public UserInfo GetUser(string loginName)
        {
            return IDAUser.Find(e => e.LoginName == loginName);
        }


        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool AddUser(UserInfo userInfo)
        {
            if (userInfo == null)
            {
                return false;
            }
            if (GetUser(userInfo.LoginName) == null)
            {
                var company = userInfo.Company;
                var com = IDACompany.Create(company);
                userInfo.CompanyID = com.CompanyID;
                userInfo.LoginPassword = Encrypt.MD5(userInfo.LoginPassword);
                IDAUser.Create(userInfo);
                IDAUser.UnitOfWork.Commit();
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool UpdateUser(UserInfo userInfo)
        {
            IDAUser.Update(userInfo);
            IDAUser.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userID">用户Id</param>
        /// <param name="password">新密码</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool ChangePassword(int userID, string password)
        {
            var user = IDAUser.Find(userID);
            if (null == user || string.IsNullOrEmpty(password))
            {
                return false;
            }
            var newPwd = Encrypt.MD5(password);
            if (newPwd == user.LoginPassword)
            {
                return true;
            }
            user.LoginPassword = newPwd;
            IDAUser.Update(user);
            IDAUser.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeleteUser(int userID)
        {
            IDAUser.Delete(userID);
            IDAUser.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 是否允许登录
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public UserInfo LoginUser(string loginName, string password)
        {
            var user = GetUser(loginName);
            if (string.IsNullOrEmpty(password)
                || user == null
                || user.LoginPassword != Encrypt.MD5(password))
            {
                return null;
            }
            return user;
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<UserInfo> LoadAll(Expression<Func<UserInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<UserInfo>[] orderByExpressions)
        {
            return IDAUser.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadCount(Expression<Func<UserInfo, bool>> where)
        {
            return IDAUser.Count(where);
        }



        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="userIds">用户ID</param>
        /// <param name="state">状态 0 未 1 通过</param>
        /// <returns></returns>
        public bool CheckUsers(IList<int> userIds, int state)
        {
            try
            {
                foreach (var userId in userIds)
                {
                    var user = IDAUser.Find(userId);
                    user.IsCheck = state;
                    IDAUser.Update(user);
                }
                IDAUser.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }


    }
}
